package com.tjec.project.companyLibrary.service.impl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.Format;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.tjec.common.constant.AttachmentConstants;
import com.tjec.common.utils.DateUtils;
import com.tjec.common.utils.SecurityUtils;
import com.tjec.common.utils.ServletUtils;
import com.tjec.common.utils.StringUtils;
import com.tjec.framework.security.service.TokenService;
import com.tjec.framework.web.domain.AjaxResult;
import com.tjec.project.system.domain.BmsAttachment;
import com.tjec.project.system.domain.BmsExpertCertification;
import com.tjec.project.system.mapper.BmsAttachmentMapper;
import com.tjec.project.system.mapper.BmsExpertCertificationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tjec.project.companyLibrary.mapper.BmsExpertCompanyMapper;
import com.tjec.project.companyLibrary.domain.BmsExpertCompany;
import com.tjec.project.companyLibrary.service.IBmsExpertCompanyService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 企业专家Service业务层处理
 * 
 * @author tjec
 * @date 2020-04-22
 */
@Service
public class BmsExpertCompanyServiceImpl implements IBmsExpertCompanyService 
{
    @Autowired
    private BmsExpertCompanyMapper bmsExpertCompanyMapper;

    @Autowired
    private BmsExpertCertificationMapper bmsExpertCertificationMapper;

    @Autowired
    private BmsAttachmentMapper bmsAttachmentMapper;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询企业专家
     * 
     * @param guid 企业专家ID
     * @return 企业专家
     */
    @Override
    public BmsExpertCompany selectBmsExpertCompanyByGuid(String guid)
    {
        return bmsExpertCompanyMapper.selectBmsExpertCompanyByGuid(guid);
    }

    /**
     * 查询企业专家列表
     * 
     * @param bmsExpertCompany 企业专家
     * @return 企业专家
     */
    @Override
    public List<BmsExpertCompany> selectBmsExpertCompanyList(BmsExpertCompany bmsExpertCompany)
    {
        return bmsExpertCompanyMapper.selectBmsExpertCompanyList(bmsExpertCompany);
    }

    /**
     * 新增企业专家
     * 
     * @param bmsExpertCompany 企业专家
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBmsExpertCompany(BmsExpertCompany bmsExpertCompany)
    {
        String expertId = UUID.randomUUID().toString();
        bmsExpertCompany.setCreateTime(DateUtils.getNowDate());
        bmsExpertCompany.setCreateBy(SecurityUtils.getLoginUser().getUser().getUserId());
        if(null==bmsExpertCompany.getGuid()||("").equals(bmsExpertCompany.getGuid())){
            bmsExpertCompany.setGuid(expertId);
        }
        parseBmsExpertCompany(bmsExpertCompany);
        int row = bmsExpertCompanyMapper.insertBmsExpertCompany(bmsExpertCompany);
        if(StringUtils.isBlank(bmsExpertCompany.getSerialNumber())){
            Format fm = new DecimalFormat("00000");//专家编号通过自动生成五位随机数
            bmsExpertCompany.setSerialNumber("H10-"+fm.format(new BigDecimal(bmsExpertCompany.getId())));
            updateBmsExpertCompany(bmsExpertCompany);
        }
        return row;
    }

    /**
     * 解析企业专家信息
     * @param bmsExpertCompany
     */
    private void parseBmsExpertCompany(BmsExpertCompany bmsExpertCompany){
        //专业分类
        if(null!=bmsExpertCompany.getProfessionCategorys()&&bmsExpertCompany.getProfessionCategorys().length>0){
            bmsExpertCompany.setProfessionCategory(StringUtils.arrayToString(bmsExpertCompany.getProfessionCategorys()));
        }
        //行业类别
        if(null!=bmsExpertCompany.getIndustryMerits()&&bmsExpertCompany.getIndustryMerits().length>0){
            bmsExpertCompany.setIndustryMerit(StringUtils.arrayToString(bmsExpertCompany.getIndustryMerits()));
        }
        //业务分类
        if(null!=bmsExpertCompany.getBusinessMerits()&&bmsExpertCompany.getBusinessMerits().length>0){
            bmsExpertCompany.setBusinessMerit(StringUtils.arrayToString(bmsExpertCompany.getBusinessMerits()));
        }
        //出生年月日转换成年龄
        if(null!=bmsExpertCompany.getBirthday()){
            bmsExpertCompany.setAge(Long.valueOf(DateUtils.yearCompare(bmsExpertCompany.getBirthday(),DateUtils.getNowDate())));
        }
        //更新专家持证情况、上传附件，每次删除原来的，添加最新的
        if(StringUtils.isNotBlank(bmsExpertCompany.getGuid())){
            bmsExpertCertificationMapper.deleteBmsExpertCertificationByExpertId(bmsExpertCompany.getGuid());
            bmsAttachmentMapper.updateBmsAttachmentByBizId(new String[]{bmsExpertCompany.getGuid()}, tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
        }
        if(!CollectionUtils.isEmpty(bmsExpertCompany.getBmsExpertCertifications())){
            for (BmsExpertCertification bmsExpertCertification : bmsExpertCompany.getBmsExpertCertifications()) {
                bmsExpertCertification.setGuid(UUID.randomUUID().toString());
                bmsExpertCertification.setExpertId(bmsExpertCompany.getGuid());
                bmsExpertCertificationMapper.insertBmsExpertCertification(bmsExpertCertification);
            }
        }
        if(!CollectionUtils.isEmpty(bmsExpertCompany.getAttachmentIds())){
            bmsAttachmentMapper.updateBmsAttachmentByGuidsUpdateBiz(bmsExpertCompany.getAttachmentIds(), bmsExpertCompany.getGuid(), AttachmentConstants.EXPERT_PERSON, tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
        }
        /*if(!CollectionUtils.isEmpty(bmsExpertCompany.getBmsAttachments())){
            ArrayList<String> attGuids = new ArrayList<String>();
            for (BmsAttachment bmsAttachment : bmsExpertCompany.getBmsAttachments()) {
                if(StringUtils.isBlank(bmsAttachment.getBizId())){
                    attGuids.add(bmsAttachment.getGuid());
                }else {
                    BmsAttachment att = new BmsAttachment();
                    att.setGuid(bmsAttachment.getGuid());
                    att.setDelFlag("0");
                    bmsAttachmentMapper.updateBmsAttachment(att);
                }
            }
            if(!CollectionUtils.isEmpty(attGuids)){
                bmsAttachmentMapper.updateBmsAttachmentByGuidsUpdateBiz(attGuids, bmsExpertCompany.getGuid(), AttachmentConstants.EXPERT_PERSON);
            }
        }*/
    }

    /**
     * 修改企业专家
     * 
     * @param bmsExpertCompany 企业专家
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBmsExpertCompany(BmsExpertCompany bmsExpertCompany)
    {
        if(StringUtils.isBlank(bmsExpertCompany.getGuid())){
            return 0;
        }
        bmsExpertCompany.setUpdateTime(DateUtils.getNowDate());
        bmsExpertCompany.setUpdateBy(SecurityUtils.getLoginUser().getUser().getUserId());
        parseBmsExpertCompany(bmsExpertCompany);
        return bmsExpertCompanyMapper.updateBmsExpertCompany(bmsExpertCompany);
    }

    /**
     * 批量删除企业专家
     * 
     * @param ids 需要删除的企业专家ID
     * @return 结果
     */
    @Override
    public int deleteBmsExpertCompanyByIds(String[] ids)
    {
        return bmsExpertCompanyMapper.deleteBmsExpertCompanyByIds(ids);
    }

    /**
     * 删除企业专家信息
     * 
     * @param id 企业专家ID
     * @return 结果
     */
    @Override
    public int deleteBmsExpertCompanyById(String id)
    {
        return bmsExpertCompanyMapper.deleteBmsExpertCompanyById(id);
    }
}
